import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

public class testJSONLib
{
    public static void main(String args[])
    {
        new testJSONLib().start();
    }

    public void start()
    {
        construct();
        parse1();
        parse2();
        parse3();
    }

    private void construct()
    {
        Map map=new HashMap();
        map.put("uuid", 100);
        map.put("name", "壮东风");
        List items=new ArrayList();
        Map item=new HashMap();
        item.put("id", 1);
        item.put("name", "物品1");
        items.add(item);
        item=new HashMap();
        item.put("id", 2);
        item.put("name", "物品2");
        items.add(item);
        map.put("items", items);
        JSONObject j=JSONObject.fromObject(map);
        System.out.println(j.toString());
    }

    private void parse1()
    {/*{{{*/
        String s="{\"uuid\":100,\"name\":\"壮东风\",\"items\":{\"a\":100,\"b\":[\"x\",\"y\",\"z\"]}}";
        //String s="{}";
        JSONObject j=JSONObject.fromObject(s);
        System.out.println(j.toString());
        if(j.has("uuid"))
        {
            Integer uuid=j.getInt("uuid");
            System.out.println("uuid: "+uuid);
        }
        else
            System.out.println("uuid is unavailable");
        if(j.has("name"))
        {
            String name=j.getString("name");
            System.out.println("name: "+name);
        }
        else
            System.out.println("name is unavailable");
        if(j.has("items"))
        {
            JSONObject items=j.getJSONObject("items");
            String ia=items.getString("a");
            System.out.println("items=>a: "+ia);
            JSONArray ib=items.getJSONArray("b");
            System.out.print("items=>b:");
            for(int i=0; i<ib.size(); ++i)
            {
                System.out.print(" "+ib.getString(i));
            }
            System.out.println("");
        }
        else
            System.out.println("items is unavailable");
    }/*}}}*/

    private void parse2()
    {/*{{{*/
        String s="{\"uuid\":100,\"name\":\"壮东风\",\"items\":[{\"id\":1,\"name\":\"物品1\"},{\"id\":2,\"name\":\"物品2\"}]}";
        JSONObject j=JSONObject.fromObject(s);
        if(j.has("items"))
        {
            JSONArray items=j.getJSONArray("items");
            for(int i=0; i<items.size(); ++i)
            {
                JSONObject item=items.getJSONObject(i);
                String id=item.getString("id");
                System.out.println("id => "+id);
                String name=item.getString("name");
                System.out.println("name => "+name);
            }
        }
        else
            System.out.println("items is unavailable");
    }/*}}}*/

    private void parse3()
    {/*{{{*/
        String s="{\"uuid\":100,\"name\":\"壮东风\",\"items\":[[1,\"物品1\"],[2,\"物品2\"]]}";
        JSONObject j=JSONObject.fromObject(s);
        if(j.has("items"))
        {
            JSONArray items=j.getJSONArray("items");
            for(int i=0; i<items.size(); ++i)
            {
                JSONArray item=items.getJSONArray(i);
                String id=item.getString(0);
                System.out.println(id);
                String name=item.getString(1);
                System.out.println(name);
            }
        }
        else
            System.out.println("items is unavailable");
    }/*}}}*/
}
